<!DOCTYPE html>
<html>

<head>
    <link rel="stylesheet" href="../css/bootstrap.min.css">
    <link rel="stylesheet" href="../css/wings.css">
    <meta name="viewport" content="width=device-width, initial-scale=1.0" charset="utf-8">
    <title>谭滨瀚的个人主页</title>
</head>

<body>
    <div>
        <nav class="navbar navbar-dark bg-dark flex-md-nowrap p-0 shadow">
            <a class="navbar-brand col-sm-3 col-md-2 mr-0" href="#">Blogs</a>
        </nav>
    </div>
    <div class="container-fluid">
        <div class="row justify-content-center">
            <nav class="icon col-md-2  d-none d-md-block bg-light" style="min-width:0px!important;">
                <div>
                    <div class="jumbotron">
                        <img src="../img/touxiang.jpg" class="rounded-circle d-block mx-auto" style="width:50px;" />
                        <p class="text-center text-info"><strong>谭滨瀚的个人主页</strong></p>
                        <p class="text-center font-weight-light">Compiled and Designed by Wings</p>
                    </div>
                    <div class="col-md-auto">
                        <ul class="nav flex-column ">
                            <li class="nav-item scale-1-1">
                                <a class="nav-link text-center" href="../index.html">
                                    <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewbox="0 0 24 24"
                                        fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"
                                        stroke-linejoin="round" class="feather feather-home">
                                        <path d="M3 9l9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"></path>
                                        <polyline points="9 22 9 12 15 12 15 22"></polyline>
                                    </svg>
                                    <span class="align-middle">Pages</span>
                                </a>
                            </li>
                            <li class="nav-item scale-1-1">
                                <a class="nav-link text-center" href="Blogs.html">
                                    <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewbox="0 0 24 24"
                                        fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"
                                        stroke-linejoin="round" class="feather feather-file">
                                        <path d="M13 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V9z"></path>
                                        <polyline points="13 2 13 9 20 9"></polyline>
                                    </svg>
                                    Blogs
                                </a>
                            </li>
                            <li class="nav-item scale-1-1">
                                <a class="nav-link text-center" href="Friend.html">
                                    <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewbox="0 0 24 24"
                                        fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"
                                        stroke-linejoin="round" class="feather feather-users">
                                        <path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"></path>
                                        <circle cx="9" cy="7" r="4"></circle>
                                        <path d="M23 21v-2a4 4 0 0 0-3-3.87"></path>
                                        <path d="M16 3.13a4 4 0 0 1 0 7.75"></path>
                                    </svg>
                                    Friends
                                </a>
                            </li>
                            <li class="nav-item scale-1-1">
                                <a class="nav-link text-center" href="Contact.html">
                                    <svg width="24" height="24" viewbox="0 0 18 18" version="1.1"
                                        xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
                                        class="si-glyph si-glyph-phone-number">
                                        <!-- Generator: Sketch 3.0.3 (7891) - http://www.bohemiancoding.com/sketch -->
                                        <title>129</title>

                                        <defs></defs>
                                        <g stroke="currentColor" stroke-width="1" stroke-linecap="round"
                                            stroke-linejoin="round" fill="none" fill-rule="evenodd">
                                            <g>
                                                <g>
                                                    <path
                                                        d="M15.924,3.779 C15.924,-0.037 9.787,0.09 7.637,0.09 C5.485,0.09 0.038,0.136 0.038,3.953 L3.947,3.953 C3.947,3.926 3.951,2.759 3.951,2.996 C3.951,2.558 4.43,1.969 5.504,1.969 L10.517,1.969 C11.563,1.969 12.054,2.558 12.054,2.996 L12.054,2.996 C12.054,3.15 12.062,3.923 12.062,3.953 L15.922,3.953 C15.922,3.94 15.924,3.791 15.924,3.779 L15.924,3.779 Z"
                                                        class="si-glyph-fill"></path>
                                                    <path
                                                        d="M4.987,9.338 C4.987,9.679 4.701,9.955 4.349,9.955 L3.652,9.955 C3.299,9.955 3.013,9.679 3.013,9.338 L3.013,8.662 C3.013,8.321 3.299,8.045 3.652,8.045 L4.349,8.045 C4.701,8.045 4.987,8.321 4.987,8.662 L4.987,9.338 L4.987,9.338 Z"
                                                        class="si-glyph-fill"></path>
                                                    <path
                                                        d="M12.975,9.345 C12.975,9.698 12.69,9.983 12.336,9.983 L11.64,9.983 C11.287,9.983 11.002,9.698 11.002,9.345 L11.002,8.648 C11.002,8.295 11.287,8.01 11.64,8.01 L12.336,8.01 C12.69,8.01 12.975,8.295 12.975,8.648 L12.975,9.345 L12.975,9.345 Z"
                                                        class="si-glyph-fill"></path>
                                                    <path
                                                        d="M8.986,9.349 C8.986,9.702 8.701,9.987 8.348,9.987 L7.65,9.987 C7.298,9.987 7.013,9.702 7.013,9.349 L7.013,8.652 C7.013,8.299 7.298,8.014 7.65,8.014 L8.348,8.014 C8.701,8.014 8.986,8.299 8.986,8.652 L8.986,9.349 L8.986,9.349 Z"
                                                        class="si-glyph-fill"></path>
                                                    <path
                                                        d="M4.962,12.336 C4.962,12.689 4.681,12.974 4.334,12.974 L3.648,12.974 C3.3,12.974 3.019,12.689 3.019,12.336 L3.019,11.639 C3.019,11.285 3.3,11 3.648,11 L4.334,11 C4.681,11 4.962,11.285 4.962,11.639 L4.962,12.336 L4.962,12.336 Z"
                                                        class="si-glyph-fill"></path>
                                                    <path
                                                        d="M12.943,12.346 C12.943,12.694 12.668,12.974 12.326,12.974 L11.65,12.974 C11.308,12.974 11.033,12.694 11.033,12.346 L11.033,11.66 C11.033,11.311 11.308,11.031 11.65,11.031 L12.326,11.031 C12.668,11.031 12.943,11.311 12.943,11.66 L12.943,12.346 L12.943,12.346 Z"
                                                        class="si-glyph-fill"></path>
                                                    <path
                                                        d="M8.982,12.346 C8.982,12.694 8.698,12.974 8.345,12.974 L7.648,12.974 C7.295,12.974 7.01,12.694 7.01,12.346 L7.01,11.66 C7.01,11.311 7.295,11.031 7.648,11.031 L8.345,11.031 C8.698,11.031 8.982,11.311 8.982,11.66 L8.982,12.346 L8.982,12.346 Z"
                                                        class="si-glyph-fill"></path>
                                                    <path
                                                        d="M4.993,15.347 C4.993,15.689 4.707,15.965 4.355,15.965 L3.658,15.965 C3.305,15.965 3.019,15.689 3.019,15.347 L3.019,14.671 C3.019,14.33 3.305,14.054 3.658,14.054 L4.355,14.054 C4.707,14.054 4.993,14.33 4.993,14.671 L4.993,15.347 L4.993,15.347 Z"
                                                        class="si-glyph-fill"></path>
                                                    <path
                                                        d="M12.975,15.357 C12.975,15.711 12.69,15.996 12.336,15.996 L11.64,15.996 C11.287,15.996 11.002,15.711 11.002,15.357 L11.002,14.66 C11.002,14.307 11.287,14.022 11.64,14.022 L12.336,14.022 C12.69,14.022 12.975,14.307 12.975,14.66 L12.975,15.357 L12.975,15.357 Z"
                                                        class="si-glyph-fill"></path>
                                                    <path
                                                        d="M8.982,15.357 C8.982,15.711 8.703,15.996 8.355,15.996 L7.669,15.996 C7.321,15.996 7.042,15.711 7.042,15.357 L7.042,14.66 C7.042,14.307 7.321,14.022 7.669,14.022 L8.355,14.022 C8.703,14.022 8.982,14.307 8.982,14.66 L8.982,15.357 L8.982,15.357 Z"
                                                        class="si-glyph-fill"></path>
                                                </g>
                                                <path
                                                    d="M0.992,5.973 L3.039,5.973 C3.039,5.973 3.746,6.201 3.926,5 L0.067,5 C0.234,6.113 0.992,5.973 0.992,5.973 L0.992,5.973 Z"
                                                    class="si-glyph-fill"></path>
                                                <path
                                                    d="M12.115,5 C12.324,6.053 13.078,5.973 13.078,5.973 L14.969,5.973 C14.969,5.973 15.727,5.984 15.912,5 L12.115,5 L12.115,5 Z"
                                                    class="si-glyph-fill"></path>
                                            </g>
                                        </g>
                                    </svg>
                                    Contact
                                </a>
                            </li>
                            <li class="nav-item scale-1-1">
                                <a class="nav-link text-center" href="Calendar.html">

                                    <svg width="24" height="24" viewbox="0 0 18 18" version="1.1"
                                        xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
                                        class="si-glyph si-glyph-calendar-1">
                                        <!-- Generator: Sketch 3.0.3 (7891) - http://www.bohemiancoding.com/sketch -->
                                        <title>1115</title>

                                        <defs></defs>
                                        <g stroke="currentColor" stroke-width="1" stroke-linecap="round"
                                            stroke-linejoin="round" fill="none" fill-rule="evenodd">
                                            <g>
                                                <path
                                                    d="M13,0.031 L13,1 L11,1 L11,0.031 L5,0.031 L5,1 L3,1 L3,0.031 L0,0.031 L0,16 L16,16 L16,0.031 L13,0.031 L13,0.031 Z M14.029,14.008 L1.955,14.008 L1.955,4.938 L14.029,4.938 L14.029,14.008 L14.029,14.008 Z"
                                                    class="si-glyph-fill"></path>
                                                <path
                                                    d="M6.027,7.957 L5.047,7.957 L5.047,7.016 L6.949,7.016 L6.949,11.954 L6.027,11.954 L6.027,7.957 L6.027,7.957 Z"
                                                    class="si-glyph-fill"></path>
                                                <path
                                                    d="M10.027,7.957 L9,7.957 L9,7.016 L10.953,7.016 L10.953,11.954 L10.027,11.954 L10.027,7.957 L10.027,7.957 Z"
                                                    class="si-glyph-fill"></path>
                                            </g>
                                        </g>
                                    </svg>
                                    Calendar
                                </a>
                            </li>
                        </ul>
                    </div>
                </div>
            </nav>
            <main role="main" class="col-md-9 ml-sm-auto col-lg-10 px-4">
                <div class="jumbotron">
                    <h1 class="text-center">游戏开发之Unity——多人游戏与网络</h1>
                    <p class="para">
                        开发3D游戏听起来门槛很高，但是Unity的出现让门槛大大降低。开发联网实时对战的3D游戏门槛就更高，因为即便熟悉掌握了Unity的开发技术，联网的游戏还要涉及到熟悉网络协议栈、掌握后端知识以及面对服务器带来的高额成本。
                    </p>
                </div>
                <div>
                    <h1 class="text-center">unity3d网络游戏开发API</h1>
                    <hr />
                    <p class="para">从 Unity 5.0 开始，提供了全新的 UNet 开发网络游戏，面向两种类型的用户：</p>
                    <ul>
                        <li>使用 Unity 开发简单多人游戏，应该从高阶 API 开始 <br>
                            <ol>
                                <li>使用 “NetworkManager” 管理网络，控制游戏对象网络状态</li>
                                <li>从客户端调用服务器函数 或 从服务器调用客户端函数</li>
                                <li>使用网络组件，可视化编辑程序</li>
                                <li>Internet 服务支持</li>
                            </ol>
                        </li>
                        <li>用户自定义网络基础设施或高级多人游戏，应该从 NetworkTransport API 开始 <br>
                            <ol>
                                <li>使用 UDP 等</li>
                                <li>自建复杂的网络结构</li>
                            </ol>
                        </li>
                    </ul>
                    <p class="para">本篇博客使用的是高级API。</p>
                    <h1 class="text-center">unity3d开发网络多人游戏</h1>
                    <hr />
                    <ol>
                        <li>新建 NetworkManager对象： <br>
                            <ul>
                                <li>首先创建一个空对象，将其命名为NetworkManager；</li>
                                <li>为该对象添加组件：NetworkManager，NetworkManagerHUB。</li>
                            </ul>
                        </li>
                        <li>制作玩家预制： <br>
                            <ul>
                                <li>为玩家预制添加组件：NetworkIdentity（Local Player
                                    Authority需要设置为true，使客户端可以控制玩家），NetworkTransform（为服务器提供玩家位置等信息）。</li>
                            </ul>
                        </li>
                        <li>注册玩家预制： <br>
                            <ul>
                                <li>将玩家预制拖入NetworkManager的swap info的Player Prefab插槽。</li>
                            </ul>
                        </li>
                        <li>注册子弹预制： <br>
                            <ul>
                                <li>新建球体对象，命名为Bullet，scale设置为0.2；</li>
                                <li>给子弹rigidbody组件，取消重力；</li>
                                <li>添加新脚本bullet.cs,bullet.cs用来处理子弹的碰撞事件；</li>
                                <li>用 Add 按钮添加一个新的 spawn 预制件，将 Bullet 预制件拖入新的 spawn 预制插槽。</li>
                            </ul>
                        </li>
                        <li>玩家控制脚本： <br>
                            <ul>
                                <li>新建名为Network.cs的玩家控制脚本；</li>
                                <li>Network.cs脚本中，控制玩家的移动和攻击，控制摄像机跟随。</li>
                                <li>新建Combat.cs，改脚本控制玩家血条值；</li>
                            </ul>
                        </li>
                        <li>添加场景控制： <br>
                            新建FirstSceneController.cs，该脚本加载摄像机和地图；</li>
                    </ol>
                    <h1 class="text-center">各cs文件代码</h1>
                    <hr />
                    <code class="language-cs hljs">
                        <ol class="hljs-ln" style="width:1052px; max-height: 500px;">
                                <li><span class="hljs-comment">/* *</span></li>
                                <li><span class="hljs-comment"> * Network.cs</span></li>
                                <li><span class="hljs-comment"> * 该脚本中控制玩家移动、攻击，控制摄像机跟随</span></li>
                                <li><span class="hljs-comment"> * */</span></li>
                                <li></li>
                                <li><span class="hljs-keyword">using</span> System.Collections;</li>
                                <li><span class="hljs-keyword">using</span> System.Collections.Generic;</li>
                                <li><span class="hljs-keyword">using</span> UnityEngine;</li>
                                <li><span class="hljs-keyword">using</span> UnityEngine.Networking;</li>
                                <li></li>
                                <li><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Network : NetworkBehaviour {</li>
                                <li>&emsp;<span class="hljs-keyword">public</span> GameObject bulletPrefab;</li>
                                <li>&emsp;<span class="hljs-keyword">public</span> Color color = Color.red;</li>
                                <li>&emsp;<span class="hljs-keyword">private</span> <span class="hljs-keyword">float</span> speed = <span class="hljs-number">3</span>;</li>
                                <li>&emsp;<span class="hljs-keyword">private</span> <span class="hljs-keyword">float</span> rspeed = <span class="hljs-number">90</span>;</li>
                                <li>&emsp;<span class="hljs-keyword">private</span> Camera myCamera;</li>
                                <li></li>
                                <li>&emsp;<span class="hljs-keyword">public</span> <span class="hljs-keyword">override</span> <span class="hljs-keyword">void</span> <span class="hljs-title">OnStartLocalPlayer</span>()</li>
                                <li>&emsp;{</li>
                                <li>&emsp;&emsp;color = Color.green;</li>
                                <li>&emsp;&emsp;myCamera = Camera.main;</li>
                                <li>&emsp;&emsp;gameObject.GetComponent&lt;Rigidbody&gt;().freezeRotation = <span class="hljs-keyword">true</span>;</li>
                                <li>&emsp;}</li>
                                <li>&emsp;<span class="hljs-comment">// Update is called once per frame</span></li>
                                <li>&emsp;<span class="hljs-keyword">void</span> Update () {</li>
                                <li>&emsp;&emsp;<span class="hljs-keyword">if</span>(isLocalPlayer)</li>
                                <li>&emsp;&emsp;{</li>
                                <li>&emsp;&emsp;&emsp;<span class="hljs-keyword">float</span> translationX = Input.GetAxis(<span class="hljs-string">"Horizontal"</span>);</li>
                                <li>&emsp;&emsp;&emsp;<span class="hljs-keyword">float</span> translationZ = Input.GetAxis(<span class="hljs-string">"Vertical"</span>);</li>
                                <li>&emsp;&emsp;&emsp;<span class="hljs-keyword">if</span> (translationX != <span class="hljs-number">0</span> || translationZ != <span class="hljs-number">0</span>)</li>
                                <li>&emsp;&emsp;&emsp;{</li>
                                <li>&emsp;&emsp;&emsp;&emsp;gameObject.GetComponent&lt;Animator&gt;().SetBool(<span class="hljs-string">"running"</span>, <span class="hljs-keyword">true</span>);</li>
                                <li>&emsp;&emsp;&emsp;}</li>
                                <li>&emsp;&emsp;&emsp;<span class="hljs-keyword">else</span></li>
                                <li>&emsp;&emsp;&emsp;{</li>
                                <li>&emsp;&emsp;&emsp;&emsp;gameObject.GetComponent&lt;Animator&gt;().SetBool(<span class="hljs-string">"running"</span>, <span class="hljs-keyword">false</span>);</li>
                                <li>&emsp;&emsp;&emsp;}</li>
                                <li>&emsp;&emsp;&emsp;gameObject.transform.Translate(translationX * speed * Time.deltaTime * <span class="hljs-number">0.1</span>f, <span class="hljs-number">0</span>, translationZ * speed * Time.deltaTime);</li>
                                <li>&emsp;&emsp;&emsp;gameObject.transform.Rotate(<span class="hljs-number">0</span>, translationX * rspeed * Time.deltaTime, <span class="hljs-number">0</span>);</li>
                                <li>&emsp;&emsp;&emsp;<span class="hljs-keyword">if</span> (gameObject.transform.localEulerAngles.x != <span class="hljs-number">0</span> || gameObject.transform.localEulerAngles.z != <span class="hljs-number">0</span>)</li>
                                <li>&emsp;&emsp;&emsp;{</li>
                                <li>&emsp;&emsp;&emsp;&emsp;gameObject.transform.localEulerAngles = <span class="hljs-keyword">new</span> Vector3(<span class="hljs-number">0</span>, gameObject.transform.localEulerAngles.y, <span class="hljs-number">0</span>);</li>
                                <li>&emsp;&emsp;&emsp;}</li>
                                <li>&emsp;&emsp;&emsp;<span class="hljs-keyword">if</span> (gameObject.transform.position.y != <span class="hljs-number">0</span>)</li>
                                <li>&emsp;&emsp;&emsp;{</li>
                                <li>&emsp;&emsp;&emsp;&emsp;gameObject.transform.position = <span class="hljs-keyword">new</span> Vector3(gameObject.transform.position.x, <span class="hljs-number">0</span>, gameObject.transform.position.z);</li>
                                <li>&emsp;&emsp;&emsp;}</li>
                                <li>&emsp;&emsp;&emsp;myCamera.transform.position = transform.position + transform.forward * -<span class="hljs-number">3</span> + <span class="hljs-keyword">new</span> Vector3(<span class="hljs-number">0</span>, <span class="hljs-number">4</span>, <span class="hljs-number">0</span>);</li>
                                <li>&emsp;&emsp;&emsp;myCamera.transform.forward = transform.forward + <span class="hljs-keyword">new</span> Vector3(<span class="hljs-number">0</span>, -<span class="hljs-number">0.5</span>f, <span class="hljs-number">0</span>);</li>
                                <li>&emsp;&emsp;&emsp;<span class="hljs-keyword">if</span> (Input.GetKeyDown(KeyCode.Space))</li>
                                <li>&emsp;&emsp;&emsp;{</li>
                                <li>&emsp;&emsp;&emsp;&emsp;gameObject.GetComponent&lt;Animator&gt;().SetBool(<span class="hljs-string">"shoot"</span>, <span class="hljs-keyword">true</span>);</li>
                                <li>&emsp;&emsp;&emsp;&emsp;CmdFire();</li>
                                <li>&emsp;&emsp;&emsp;&emsp;gameObject.GetComponent&lt;Animator&gt;().SetBool(<span class="hljs-string">"shoot"</span>, <span class="hljs-keyword">false</span>);</li>
                                <li>&emsp;&emsp;&emsp;}</li>
                                <li>&emsp;&emsp;}</li>
                                <li>&emsp;}</li>
                                <li>&emsp;[Command]</li>
                                <li>&emsp;<span class="hljs-keyword">void</span> CmdFire()</li>
                                <li>&emsp;{</li>
                                <li>&emsp;&emsp;<span class="hljs-comment">// This [Command] code is run on the server!</span></li>
                                <li></li>
                                <li>&emsp;&emsp;<span class="hljs-comment">// create the bullet object locally</span></li>
                                <li>&emsp;&emsp;<span class="hljs-keyword">var</span> bullet = (GameObject)Instantiate(bulletPrefab,transform.position + transform.forward + <span class="hljs-keyword">new</span> Vector3(<span class="hljs-number">0</span>,<span class="hljs-number">1</span>,<span class="hljs-number">0</span>),Quaternion.identity);</li>
                                <li></li>
                                <li>&emsp;&emsp;bullet.GetComponent&lt;Rigidbody&gt;().velocity = transform.forward * <span class="hljs-number">4</span>;</li>
                                <li></li>
                                <li>&emsp;&emsp;<span class="hljs-comment">// spawn the bullet on the clients</span></li>
                                <li>&emsp;&emsp;NetworkServer.Spawn(bullet);</li>
                                <li></li>
                                <li>&emsp;&emsp;<span class="hljs-comment">// when the bullet is destroyed on the server it will automaticaly be destroyed on clients</span></li>
                                <li>&emsp;&emsp;Destroy(bullet, <span class="hljs-number">2.0</span>f);</li>
                                <li>&emsp;}</li>
                                <li>}</li>
                        </ol>
                    </code>
                    <hr />
                    <code class="language-cs hljs">
                        <ol class="hljs-ln" style="width:1052px; max-height: 500px;">
                            <li><span class="hljs-comment">/***</span></li>
                            <li><span class="hljs-comment"> * Bullet.cs</span></li>
                            <li><span class="hljs-comment"> * 该脚本处理子弹的碰撞</span></li>
                            <li><span class="hljs-comment"> * */</span></li>
                            <li></li>
                            <li><span class="hljs-keyword">using</span> System.Collections;</li>
                            <li><span class="hljs-keyword">using</span> System.Collections.Generic;</li>
                            <li><span class="hljs-keyword">using</span> UnityEngine;</li>
                            <li></li>
                            <li><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Bullet : MonoBehaviour {</li>
                            <li>&emsp;<span class="hljs-keyword">void</span> OnCollisionEnter(Collision collision)</li>
                            <li>&emsp;{</li>
                            <li>&emsp;&emsp;<span class="hljs-keyword">var</span> hit = collision.gameObject;</li>
                            <li>&emsp;&emsp;<span class="hljs-keyword">var</span> hitPlayer = hit.GetComponent&lt;Network&gt;();</li>
                            <li></li>
                            <li>&emsp;&emsp;<span class="hljs-keyword">if</span> (hitPlayer != <span class="hljs-keyword">null</span>)</li>
                            <li>&emsp;&emsp;{</li>
                            <li>&emsp;&emsp;&emsp;<span class="hljs-keyword">var</span> combat = hit.GetComponent&lt;Combat&gt;();</li>
                            <li>&emsp;&emsp;&emsp;combat.TakeDamage(<span class="hljs-number">30</span>);</li>
                            <li>&emsp;&emsp;}</li>
                            <li>&emsp;&emsp;Destroy(gameObject);</li>
                            <li>&emsp;}</li>
                            <li>}</li>
                        </ol>
                    </code>
                    <hr />
                    <code class="language-cs hljs">
                        <ol class="hljs-ln" style="width:1052px; max-height: 500px;">
                                    <li><span class="hljs-comment">/***</span></li>
                                    <li><span class="hljs-comment"> * Combat.cs</span></li>
                                    <li><span class="hljs-comment"> * 该脚本控制人物血量和人物死亡的重生</span></li>
                                    <li><span class="hljs-comment"> * */</span></li>
                                    <li></li>
                                    <li><span class="hljs-keyword">using</span> UnityEngine;</li>
                                    <li><span class="hljs-keyword">using</span> UnityEngine.Networking;</li>
                                    <li></li>
                                    <li><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Combat : NetworkBehaviour</li>
                                    <li>{</li>
                                    <li>&emsp;<span class="hljs-keyword">public</span> <span class="hljs-keyword">const</span> <span class="hljs-keyword">int</span> maxHealth = <span class="hljs-number">100</span>;</li>
                                    <li>&emsp;[SyncVar]</li>
                                    <li>&emsp;<span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> health = maxHealth;</li>
                                    <li></li>
                                    <li>&emsp;<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">TakeDamage</span>(<span class="hljs-keyword">int</span> amount)</li>
                                    <li>&emsp;{</li>
                                    <li>&emsp;&emsp;<span class="hljs-keyword">if</span> (!isServer)</li>
                                    <li>&emsp;&emsp;&emsp;<span class="hljs-keyword">return</span>;</li>
                                    <li></li>
                                    <li>&emsp;&emsp;health -= amount;</li>
                                    <li>&emsp;&emsp;<span class="hljs-keyword">if</span> (health &lt;= <span class="hljs-number">0</span>)</li>
                                    <li>&emsp;&emsp;{</li>
                                    <li>&emsp;&emsp;&emsp;health = <span class="hljs-number">100</span>;</li>
                                    <li>&emsp;&emsp;&emsp;GetComponent&lt;Animator&gt;().SetBool(<span class="hljs-string">"death"</span>, <span class="hljs-keyword">true</span>);</li>
                                    <li>&emsp;&emsp;&emsp;GetComponent&lt;Animator&gt;().SetBool(<span class="hljs-string">"death"</span>, <span class="hljs-keyword">false</span>);</li>
                                    <li>&emsp;&emsp;&emsp;RpcRespawn();</li>
                                    <li>&emsp;&emsp;}</li>
                                    <li>&emsp;}</li>
                                    <li></li>
                                    <li>&emsp;[ClientRpc]</li>
                                    <li>&emsp;<span class="hljs-keyword">void</span> RpcRespawn()</li>
                                    <li>&emsp;{</li>
                                    <li>&emsp;&emsp;<span class="hljs-keyword">if</span> (isLocalPlayer)</li>
                                    <li>&emsp;&emsp;{</li>
                                    <li>&emsp;&emsp;&emsp;<span class="hljs-comment">// move back to zero location</span></li>
                                    <li>&emsp;&emsp;&emsp;transform.position = Vector3.zero;</li>
                                    <li>&emsp;&emsp;}</li>
                                    <li>&emsp;}</li>
                                    <li>}</li>
                        </ol>
                    </code>
                    <hr />
                    <code class="language-cs hljs">
                        <ol class="hljs-ln" style="width:1052px; max-height: 500px;">
                                <li><span class="hljs-comment">/***</span></li>
                                    <li><span class="hljs-comment"> * HealthBar.cs</span></li>
                                    <li><span class="hljs-comment"> * 该脚本用于控制玩家血条显示，区分敌我</span></li>
                                    <li><span class="hljs-comment"> * */</span></li>
                                    <li></li>
                                    <li><span class="hljs-keyword">using</span> UnityEngine;</li>
                                    <li><span class="hljs-keyword">using</span> System.Collections;</li>
                                    <li></li>
                                    <li><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> HealthBar : MonoBehaviour</li>
                                    <li>{</li>
                                    <li>&emsp;GUIStyle healthStyle;</li>
                                    <li>&emsp;GUIStyle backStyle;</li>
                                    <li>&emsp;Combat combat;</li>
                                    <li>&emsp;Color barColor;</li>
                                    <li></li>
                                    <li>&emsp;<span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Start</span>()</li>
                                    <li>&emsp;{</li>
                                    <li>&emsp;&emsp;combat = GetComponent&lt;Combat&gt;();</li>
                                    <li>&emsp;&emsp;barColor = GetComponent&lt;Network&gt;().color;</li>
                                    <li>&emsp;}</li>
                                    <li></li>
                                    <li>&emsp;<span class="hljs-keyword">void</span> OnGUI()</li>
                                    <li>&emsp;{</li>
                                    <li>&emsp;&emsp;InitStyles();</li>
                                    <li></li>
                                    <li>&emsp;&emsp;<span class="hljs-comment">// Draw a Health Bar</span></li>
                                    <li></li>
                                    <li>&emsp;&emsp;Vector3 pos = Camera.main.WorldToScreenPoint(transform.position);</li>
                                    <li></li>
                                    <li>&emsp;&emsp;<span class="hljs-comment">// draw health bar background</span></li>
                                    <li>&emsp;&emsp;GUI.color = Color.grey;</li>
                                    <li>&emsp;&emsp;GUI.backgroundColor = Color.grey;</li>
                                    <li>&emsp;&emsp;GUI.Box(<span class="hljs-keyword">new</span> Rect(pos.x - <span class="hljs-number">26</span>, Screen.height - pos.y + <span class="hljs-number">20</span>, Combat.maxHealth / <span class="hljs-number">2</span>, <span class="hljs-number">7</span>), <span class="hljs-string">"."</span>, backStyle);</li>
                                    <li></li>
                                    <li>&emsp;&emsp;<span class="hljs-comment">// draw health bar amount</span></li>
                                    <li>&emsp;&emsp;GUI.color = color;</li>
                                    <li>&emsp;&emsp;GUI.backgroundColor = color;</li>
                                    <li>&emsp;&emsp;GUI.Box(<span class="hljs-keyword">new</span> Rect(pos.x - <span class="hljs-number">25</span>, Screen.height - pos.y + <span class="hljs-number">21</span>, combat.health / <span class="hljs-number">2</span>, <span class="hljs-number">5</span>), <span class="hljs-string">"."</span>, healthStyle);</li>
                                    <li>&emsp;}</li>
                                    <li></li>
                                    <li>&emsp;<span class="hljs-keyword">void</span> InitStyles()</li>
                                    <li>&emsp;{</li>
                                    <li>&emsp;&emsp;<span class="hljs-keyword">if</span> (healthStyle == <span class="hljs-keyword">null</span>)</li>
                                    <li>&emsp;&emsp;{</li>
                                    <li>&emsp;&emsp;&emsp;healthStyle = <span class="hljs-keyword">new</span> GUIStyle(GUI.skin.box);</li>
                                    <li>&emsp;&emsp;&emsp;healthStyle.normal.background = MakeTex(<span class="hljs-number">2</span>, <span class="hljs-number">2</span>, color);</li>
                                    <li>&emsp;&emsp;}</li>
                                    <li></li>
                                    <li>&emsp;&emsp;<span class="hljs-keyword">if</span> (backStyle == <span class="hljs-keyword">null</span>)</li>
                                    <li>&emsp;&emsp;{</li>
                                    <li>&emsp;&emsp;&emsp;backStyle = <span class="hljs-keyword">new</span> GUIStyle(GUI.skin.box);</li>
                                    <li>&emsp;&emsp;&emsp;backStyle.normal.background = MakeTex(<span class="hljs-number">2</span>, <span class="hljs-number">2</span>, <span class="hljs-keyword">new</span> Color(<span class="hljs-number">0</span>f, <span class="hljs-number">0</span>f, <span class="hljs-number">0</span>f, <span class="hljs-number">1.0</span>f));</li>
                                    <li>&emsp;&emsp;}</li>
                                    <li>&emsp;}</li>
                                    <li></li>
                                    <li>&emsp;Texture2D MakeTex(<span class="hljs-keyword">int</span> width, <span class="hljs-keyword">int</span> height, Color col)</li>
                                    <li>&emsp;{</li>
                                    <li>&emsp;&emsp;Color[] pix = <span class="hljs-keyword">new</span> Color[width * height];</li>
                                    <li>&emsp;&emsp;<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; pix.Length; ++i)</li>
                                    <li>&emsp;&emsp;{</li>
                                    <li>&emsp;&emsp;&emsp;pix[i] = col;</li>
                                    <li>&emsp;&emsp;}</li>
                                    <li>&emsp;&emsp;Texture2D result = <span class="hljs-keyword">new</span> Texture2D(width, height);</li>
                                    <li>&emsp;&emsp;result.SetPixels(pix);</li>
                                    <li>&emsp;&emsp;result.Apply();</li>
                                    <li>&emsp;&emsp;<span class="hljs-keyword">return</span> result;</li>
                                    <li>&emsp;}</li>
                                    <li>}</li>
                        </ol>
                    </code>
                </div>
            </main>
        </div>

    </div>
    <script src="../js/jquery-3.3.1.slim.min.js"></script>
    <script src="../js/popper.min.js"></script>
    <script src="../js/bootstrap.js"></script>

</body>

</html>